package com.example.administrator.android01app;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Ch5Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch5_1);
    }

    public void toast1(View view){
        //Toast toast=Toast.makeText(this,"message",Toast.LENGTH_LONG);
        //toast.show();

        Toast.makeText(this,"message1",Toast.LENGTH_LONG).show();

    }

    public void toast2(View view){
        Toast toast=Toast.makeText(this,"message2",Toast.LENGTH_LONG);
        toast.setGravity(Gravity.LEFT|Gravity.TOP,0,0);//如果有多个位置常量，使用|隔开
        toast.show();
    }

    public void toast3(View view){
        LayoutInflater layoutInflater=getLayoutInflater();
        View view1=layoutInflater.inflate(R.layout.layout_toast,null);//借助LayoutInflater，将自定义的布局文件转换为View类的实例
        //设置界面上的输出内容
        ImageView imageView=(ImageView) view1.findViewById(R.id.toast_iv);
        imageView.setImageResource(R.drawable.abcd);
        TextView textView=(TextView)view1.findViewById(R.id.toast_tv);
        textView.setText("toast3");

        Toast toast=new Toast(this);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);//设置显示的时长
        toast.setView(view1);//调用setView方法，将自定义的界面加载
        toast.show();
    }

    public void notification(View view){
        //使用构造器进行通知基本属性设置
        Notification.Builder builder=new Notification.Builder(this);
        builder.setContentTitle("title");
        builder.setContentText("message");
        builder.setWhen(System.currentTimeMillis());
        builder.setSmallIcon(android.R.drawable.ic_delete);

        Notification notification=builder.build();//通过build方法得到Notification实例

        NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(123,notification);

    }

    public void cancelNotification(View view){
        //取消通知
        NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(123);
    }

    public void alertDialog(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);//利用Builder设置对话框的属性
        builder.setTitle("title");
        builder.setMessage("message");
        builder.setIcon(android.R.drawable.ic_dialog_email);
        builder.setPositiveButton("accpet", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(Ch5Activity1.this,"accpet",Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("reject", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(Ch5Activity1.this,"reject",Toast.LENGTH_LONG).show();
            }
        });
        builder.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(Ch5Activity1.this,"cancel",Toast.LENGTH_LONG).show();
            }
        });
        //调用create方法创建AlertDialog的实例，再调用 show方法进行展示
        builder.create().show();
    }

}
